<?php
/**
 * Created by PhpStorm.
 * User: 万奇
 * Date: 2020/11/29 0029
 * Time: 23:15
 */

namespace app\admin\controller\v1;

use app\Request;
use think\App;
use think\facade\Db;

class WxkOrder extends BasicController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }
    /**
     * @notes  创建订单
     */
    public function order_add(){
        $param = $this->param;
        $id = $param['id'];
        if(empty($id)){
            response(500, '缺少id');
        }
        $customer = Db::name('wxk_customer')->find($id);
        if(empty($customer)){
            response(500, '线索错误');
        }
        $data = [
            'leads_id' => $customer['id'],
            'external_user_id' => $customer['external_user_id'],
            'follow_userid' => $customer['follow_userid'],
            'account_id' => $param['account_id'],
            'open_date' => $param['open_date'],
            'first_deposit' => $param['first_deposit'],
            'ad_fee' => $param['ad_fee'],
            'service_fee' => $param['service_fee'],
            'deal_record'=> $param['deal_record'],
            'create_time' => date("Y-m-d H:i:s",time())
        ];
        $order_id = Db::name('wxk_order')->insertGetId($data);
        if($order_id){
            Db::name('wxk_customer')->where('id',$id)->update(['follow_status'=>4]);
            response(200, '操作成功');
        }else{
            response(500, '操作失败');
        }
    }

    /**
     * @notes  订单列表
     */
    public function get_order_list(){
        $where = [];
        $admin = $this->user_info;
        if ($admin['role_id'] == '106a9731-838e-23d3-0fd3-b0800dd352bf') {
            $where[] = ['o.follow_userid', '=', $admin['userid']];
        }
        if ($admin['role_id'] == 'b9115062-e965-a34d-ed14-9fb7f9a742a3') {
            $where[] = ['o.follow_userid', 'in', ['huayunlong','zhangguoqing','wangshuo']];
        }
        $param = $this->param;
        if (is_exists($param['keyword'])) {
            $where[] = ['c.name|c.follow_remark|c.follow_remark_company|c.follow_remark_mobiles', 'like', "%{$param['keyword']}%"];
        }
        $list = Db::name('wxk_order')->alias('o')
            ->leftJoin('wxk_customer c', 'c.external_user_id = o.external_user_id')
            ->leftJoin('wxk_customer_portrait p', 'p.external_user_id = o.external_user_id')
            ->leftJoin('sys_user u', 'u.userid = o.follow_userid')
            ->field('o.id as order_id,o.*,
            c.name as nickname,c.avatar,c.follow_remark as real_name,c.follow_remark_company,c.follow_remark_mobiles,c.follow_createtime as create_time,
            p.industry,p.area,u.username as admin_name')
            ->where($where)
            ->order(['o.id' => 'desc'])
            ->paginate($param['limit'])->toArray();

        foreach ($list['data'] as $k => $v){
            unset($list['data'][$k]['id']);
            unset($list['data'][$k]['status']);
        }
        response(200, '', $list['data'], $list['total']);
    }
    /**
     * @notes  订单详情
     */
    public function get_order_info(){
        $order_id = $this->param['order_id'];

        $data = Db::name('wxk_order')->alias('o')
            ->leftJoin('wxk_customer c', 'c.external_user_id = o.external_user_id')
            ->leftJoin('wxk_customer_portrait p', 'p.external_user_id = o.external_user_id')
            ->leftJoin('wxk_staff s', 's.user_id = o.follow_userid')
            ->field('o.id as order_id,o.*,c.name as nickname,c.avatar,c.follow_remark as real_name,c.follow_remark_company,c.follow_remark_mobiles,p.industry,p.area,s.name as admin_name')
            ->where('o.id', $order_id)
            ->find();
        if($data){
            unset($data['id']);
            response(200, '', $data);
        }else{
            response(500, '请求失败');
        }
    }
    /**
     * @notes  补充订单资料
     */
    public function order_update(){
        $param = $this->param;
        $order_id = $param['order_id'];

        if(empty($order_id)){
            response(500, '缺少订单id');
        }
        $data = [
            'account_id' => $param['account_id'],
            'open_date' => $param['open_date'],
            'first_deposit' => $param['first_deposit'],
            'ad_fee' => $param['ad_fee'],
            'service_fee' => $param['service_fee'],
            'deal_record'=> $param['deal_record'],
            'update_time' => date("Y-m-d H:i:s",time())
        ];
        $rows = Db::name('wxk_order')->where('id',$order_id)->update($data);
        if($rows){
            response(200, '操作成功');
        }else{
            response(500, '操作失败');
        }
    }
    /**
     * @notes  上传图片
     */
    public function upload_images(){
        $time = date('YmdHis');
        if ($_FILES["file"]["error"]) {
            return rsp(500, $_FILES["file"]["error"]);
        }
        $end_name = strtoupper(mb_substr(strrchr($_FILES["file"]["name"], '.'), 1));
        if(($_FILES["file"]["type"] == "image/png" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/jpg") && $_FILES["file"]["size"] < 2048000) {
            $filename = './uploads/'.$time. '.jpg';
            $type = 'image';
        }else{
            return rsp(500, '此类型不支持上传');
        }
        $filename = iconv("UTF-8", "gb2312", $filename);
        move_uploaded_file($_FILES["file"]["tmp_name"], $filename);
        $url = \think\facade\Request::domain() . mb_substr($filename, 1);
        if($url){
            return rsp(200, '成功', ['content' => $url, 'file_name' => $_FILES['file']['name'], 'file_suffix' => $end_name]);
        }else{
            return rsp(500, '上传失败');
        }
    }
}